package com.kly.sc.api.application.controller.other;

import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.api.Response;
import com.kly.sc.api.application.annos.SignIgnore;
import com.kly.sc.api.application.annos.VisitorAccessible;
import com.kly.sc.api.application.controller.other.request.CommonLogRequest;
import com.kly.sc.api.application.helper.RequestLocalInfo;
import com.kly.sc.api.application.service.utils.BaseComponent;
import com.kly.sc.api.infras.app.RequestBasicInfo;
import com.kly.sc.api.infras.utils.StringUtils;
import com.kly.user.dto.CommonLogReq;
import com.kly.user.service.OtherService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 该口用于接收通用数据
 */
@RestController
@RequestMapping("api/common")
public class OtherController extends BaseComponent {

    @Resource
    private OtherService otherService;

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @ResponseBody
    @RequestMapping(value = "/receive/common/notify", method = RequestMethod.POST)
    @SignIgnore
    public Response receiveNotify(@RequestBody() CommonLogRequest commonLogRequest) {
        if (Objects.isNull(commonLogRequest)) {
            return Response.ok();
        }
        final String userCode = commonLogRequest.getUserCode();
        final RequestBasicInfo requestBasicInfo = RequestLocalInfo.getRequestBasicInfo();
        try {
//            logger.info("====receiveCommonNofify==== params :{}", JSONObject.toJSONString(commonLogRequest));
            CommonLogReq commonLogReq = CommonLogReq.builder().userCode(userCode).build();
            BeanUtils.copyProperties(commonLogRequest, commonLogReq);
            commonLogReq.setAppVersionName(requestBasicInfo.getAppVersionName());
            commonLogReq.setAppVersion(Integer.valueOf(requestBasicInfo.getAppVersion()));
            otherService.storeCommonLog(commonLogReq);
//            logger.info("====receiveCommonNofifySuccess==== params :{}", commonLogRequest);
        } catch (Exception e) {
            logger.info("====receiveCommonNofifyFailed====", e);
        }

        return Response.ok();

    }


}
